package com.tuocent.dagv2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuocent.dagv2.common.AjaxResult;
import com.tuocent.dagv2.common.entity.LoginUserInfo;
import com.tuocent.dagv2.common.utils.SnowFlakeID;
import com.tuocent.dagv2.common.utils.redis.RedisCache;
import com.tuocent.dagv2.constant.PublicConst;
import com.tuocent.dagv2.entity.Customer;
import com.tuocent.dagv2.mapper.CustomerMapper;
import com.tuocent.dagv2.service.ICustomerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 * 客户信息 服务实现类
 * </p>
 *
 * @author Vincent
 * @since 2025-08-19
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements ICustomerService {

    @Resource
    private SnowFlakeID snowFlakeID;
    @Resource
    private RedisCache redis;

    @Override
    public String setCustomerInfo(Model model, String id) {
        Customer data = getById(id);
        if (data == null) {
            data = new Customer();
        }
        model.addAttribute("data", data);
        return "/caes/setCustomerInfo";
    }

    @Override
    public AjaxResult saveCustomerInfo(HttpSession session, Customer data) {
        if (data == null) {
            return AjaxResult.error("没有可保存的数据");
        }
        LoginUserInfo loginUser = redis.get(session.getId());
        if (data.getId() == null || data.getId().isEmpty()) {
            data.setId(snowFlakeID.getId());
            data.setCreateTime(LocalDateTime.now());
            data.setCreator(loginUser.getUserName());
            data.setIsDel("0");
        } else {
            data.setModifyTime(LocalDateTime.now());
            data.setModifier(loginUser.getUserName());
        }
        if (count(new QueryWrapper<Customer>()
                .eq("code", data.getCode())
                .eq("is_del", "0")
                .ne("id", data.getId())) > 0) {
            return AjaxResult.error("客户编号已存在");
        }
        return saveOrUpdate(data) ? AjaxResult.success("保存客户信息成功") : AjaxResult.error("保存客户信息失败");
    }

    @Override
    public AjaxResult getCustomerData(Map<String, Object> param) {
        int page = (int) param.getOrDefault("page", PublicConst.DEF_PAGE);
        int limit = (int) param.getOrDefault("limit", PublicConst.DEF_LIMIT);
        QueryWrapper<Customer> qw = new QueryWrapper<Customer>()
                .eq("is_del", "0");
        for (String key : param.keySet()) {
            String value = param.getOrDefault(key, "").toString();
            switch (key) {
                case "page":
                case "limit":
                    break;
                case "name":
                case "premise_address":
                case "post_address":
                    if (!"".equals(value)) {
                        qw.like(key, value);
                    }
                    break;
                default:
                    if (!"".equals(value)) {
                        qw.eq(key, value);
                    }
            }
        }
        Page<Customer> data = page(new Page<>(page, limit), qw);
        return AjaxResult.success(data.getRecords(), data.getTotal());
    }

    @Override
    public AjaxResult delCustomer(HttpSession session, Map<String, String> data) {
        String id = data.getOrDefault("id", "");
        LoginUserInfo loginUser = redis.get(session.getId());
        UpdateWrapper uw = new UpdateWrapper<>()
                .set("is_del", "1")
                .set("modifier", loginUser.getUserName())
                .set("modify_time", LocalDateTime.now())
                .eq("id", id);
        return update(uw) ? AjaxResult.success("删除客户信息成功") : AjaxResult.error("删除客户信息失败");
    }
}
